﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;
using System.Data.SqlClient;

namespace ProjectX
{
    public partial class mybuy : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["userNick"] != null)
                    {
                        getTotalCount();
                        bindmyview();
                        bindaddview();
                        this.username.Text = Session["userNick"].ToString();
                        this.loginlink.Visible = false;
                        this.reglink.Visible = false;
                        this.logout.Visible = true;
                    }
                    else
                    {
                        Response.Redirect("/Login.html");
                    }
                }
                catch (Exception)
                {
                    
                }
            }
        }

        private void getTotalCount()
        {
            SqlDbHelper sdh = new SqlDbHelper();
            string sqlcount = "SELECT COUNT(*) FROM BuyersinfoR WHERE [Status] = 0 AND ReleaseName='"+ Session["userNick"] +"'";
            this.AspNetPagermybuy.RecordCount = int.Parse(sdh.ExecuteScalar(sqlcount).ToString());
            string sqlcount1 = "SELECT COUNT(*) FROM BuyersinfoR WHERE [Status] = 0 AND ReleaseName<>'" + Session["userNick"] + "' AND CHARINDEX('" + Session["userNick"] + "',Responder) > 0";
            this.AspNetPagermyadd.RecordCount = int.Parse(sdh.ExecuteScalar(sqlcount1).ToString());
        }

        private void bindmyview()
        {
            SqlDbHelper sdh = new SqlDbHelper();
            string sqlwhere1 = " WHERE [Status] = 0 AND ReleaseName='" + Session["userNick"] + "'";
            
            SqlParameter p1 = new SqlParameter("@tablename", "BuyersinfoR");//视图名或表名
            SqlParameter p2 = new SqlParameter("@pagerow", this.AspNetPagermybuy.PageSize);         //每页显示记录数 
            SqlParameter p3 = new SqlParameter("@pagenow", this.AspNetPagermybuy.CurrentPageIndex); //当前显示第几页 
            SqlParameter p4 = new SqlParameter("@ordercol", "Starttime");//排序字段
            SqlParameter p5 = new SqlParameter("@order", 1);//排序顺序，0升序，1降序
            SqlParameter p6 = new SqlParameter("@where", sqlwhere1);
            SqlParameter[] paras = new SqlParameter[] { p1, p2, p3, p4, p5, p6 };

            this.mybuyview.DataSource = sdh.ExecuteDataTable("sp_page", System.Data.CommandType.StoredProcedure, null, paras);
            this.mybuyview.DataBind();          
        }

        private void bindaddview()
        {
            SqlDbHelper sdh = new SqlDbHelper();
            string sqlwhere2 = " WHERE [Status] = 0 AND ReleaseName<>'" + Session["userNick"] + "' AND CHARINDEX('" + Session["userNick"] + "',Responder) > 0";
            SqlParameter s1 = new SqlParameter("@tablename", "BuyersinfoR");//视图名或表名
            SqlParameter s2 = new SqlParameter("@pagerow", this.AspNetPagermyadd.PageSize);         //每页显示记录数 
            SqlParameter s3 = new SqlParameter("@pagenow", this.AspNetPagermyadd.CurrentPageIndex); //当前显示第几页 
            SqlParameter s4 = new SqlParameter("@ordercol", "Starttime");//排序字段
            SqlParameter s5 = new SqlParameter("@order", 1);//排序顺序，0升序，1降序
            SqlParameter s6 = new SqlParameter("@where", sqlwhere2);
            SqlParameter[] paras2 = new SqlParameter[] { s1, s2, s3, s4, s5, s6 };
            this.myaddview.DataSource = sdh.ExecuteDataTable("sp_page", System.Data.CommandType.StoredProcedure, null, paras2);
            this.myaddview.DataBind();
        }

        protected void AspNetPagermybuy_PageChanged(object sender, EventArgs e)
        {
            bindmyview();
            ScriptManager.RegisterStartupScript(AspNetPagermybuy, this.GetType(), "resizeimg", "<script>ResizeImg();</script>", false);
        }

        protected void AspNetPagermyadd_PageChanged(object sender, EventArgs e)
        {
            bindaddview();
            ScriptManager.RegisterStartupScript(AspNetPagermyadd, this.GetType(), "resizeimg", "<script>ResizeImg();</script>", false);
            ScriptManager.RegisterStartupScript(AspNetPagermyadd, this.GetType(), "showContent2", "<script>$('#Content2').show();$('#Content1').hide();</script>", false);
        }
        protected void logout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Write("<script language='javascript'>window.parent.location.href='/'</script>");
        }
    }
}